<?php
namespace Blind\Controller;
use Think\Controller;
class AdvertisementController extends Controller {

    //获取所有广告
    public function index()
    {
        $data = M('advertisement') -> select();
        $this -> assign('data', $data);
        $this -> display();
    }

    public function addhtml()
    {
        $this -> display();
    }
    //添加广告
    public function addAdver()
    {
        $data['url'] = I('post.url');
        $imgInfo = upload();
        $data['img'] = $imgInfo['image']['savepath'].$imgInfo['image']['savename'];

        $res = M('advertisement') -> add($data);
        if($res)
        {
            $this -> success('添加成功', 'index');
        }else
        {
            $this -> error('添加失败');
        }
    }

    //删除广告
    public function delAdver($id)
    {
        $res = M('advertisement') -> where("id=%d",$id) -> delete();
        if($res)
        {
            $this -> success('成功');
        }else
        {
            $this -> error('失败');
        }
    }

    //编辑广告
    public function updAdver()
    {
        $data = I('post.');
        $id = $data['id'];
        $res = M('advertisement') ->where("id=%d",$id)-> save($data);
        if($res)
        {
            $this -> success('保存成功', 'index');
        }else
        {
            $this -> error('保存失败');
        }
    }

    //编辑时获取单个广告的信息
    //
    public function findAdver($id)
    {

        $res = M('advertisement') -> where("id=%d",$id) -> select();
        if($res)
        {
            $this -> assign('data', $res);
            $this -> display();
        }else
        {
            $this -> error('编辑失败');
        }

    }





}